﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using MuddyBoots.GreenlightQC.Mobile.Persistence;
using MuddyBoots.GreenlightQC.DataService;
using System.Collections.Generic;
using MuddyBoots.GreenlightQC.DataService.Enumeration;

namespace DataServiceTests
{
    [TestClass]
    public class LoginTest
    {
        [TestMethod]
        public void ValidCredentials()
        {
            // arrange
            QRY_User_Validate_Result userValidateResult = new QRY_User_Validate_Result
            {
                UserID = 1,
                FullName = "Fred Flintstone",
                UserLevel = 1,
                UserName = "fred",
                CountryLocaleID = 1,
                StringCode = "en-US"
            };

            var collection = new[] { userValidateResult };

            var mockDatabaservice = new Mock<IDatabaseEntities>();
            mockDatabaservice.Setup(x => x.ValidateCredentials(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(collection);
            IGreenlightQCMobile dataService = new GreenlightQCMobile();

            // act 
            var user = dataService.LogIn(mockDatabaservice.Object, "username", "password");

            // assert
            Assert.AreEqual(UserAccountStatus.Valid, user.AccountStatus);
        }

        [TestMethod]
        [ExpectedException(typeof(UnauthorizedAccessException))]
        public void InvalidCredentials()
        {
            // arrange            
            var mockDatabaservice = new Mock<IDatabaseEntities>();
            mockDatabaservice.Setup(x => x.ValidateCredentials(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(new List<QRY_User_Validate_Result>());
            IGreenlightQCMobile dataService = new GreenlightQCMobile();

            // act
            var result = dataService.LogIn(mockDatabaservice.Object, "username", "password");
        }

        [TestMethod]
        public void LockedOut()
        {
            // arrange
            QRY_User_Validate_Result userValidateResult = new QRY_User_Validate_Result
                {
                    UserID = 1,
                    FullName = "Fred Flintstone",
                    UserLevel = 1,
                    UserName = "fred",
                    CountryLocaleID = 1,
                    StringCode = "en-US"
                };

            var collection = new[] { userValidateResult, userValidateResult, userValidateResult };

            var mockDatabaservice = new Mock<IDatabaseEntities>();
            mockDatabaservice.Setup(x => x.ValidateCredentials(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(collection);
            mockDatabaservice.Setup(x => x.GetFailedLoginCount(It.IsAny<int>())).Returns(5);

            IGreenlightQCMobile dataService = new GreenlightQCMobile();

            // act
            var result = dataService.LogIn(mockDatabaservice.Object, "username", "password");

            // assert
            Assert.AreEqual(UserAccountStatus.Locked, result.AccountStatus);
        }
    }
}
